/*
 * Thread Pojo Class
 * 
 * This is the thread object as it is stored in the database, @tags added for synchronization with database fields.
 */

package airport.forum.pojo;

import java.util.Date;
import java.util.Set;
import java.util.HashSet;

import airport.forum.pojo.Post;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.GeneratedValue;
import javax.persistence.ManyToOne;
import javax.persistence.JoinColumn;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
//thread
@Entity
@Table(name ="thread")
public class Thread {
	//test push
	@Id
	@GeneratedValue
	@Column(name = "thread_id")
	private Long threadId;
	
	@Column(name = "user_name")
	private String userName;
	
	@Column(name = "thread_title")
	private String threadTitle;
	
	@Temporal(TemporalType.DATE) 
	@Column(name = "date_created")
	private Date dateCreated;
	
	@Column(name = "post_count")
	private Long postCount;
	
	@Column(name = "tags")
	private String tags;
	
	@Column(name = "opening_post")
	private String openingPost;
	
	@OneToMany(targetEntity = Post.class, mappedBy = "thread", cascade = CascadeType.ALL, fetch = FetchType.LAZY) //'thread' is an member variable in POJO thread
	@OrderBy (value = "postId ASC")
	private Set<Post> postlist = new HashSet<Post>();
	public Set<Post> getPostlist() {
		return postlist;
	}
	public void setPostlist(Set<Post> postlist) {
		this.postlist = postlist;
	}
	public void addPost(Post post) {
		
		System.out.println("***<Thread POJO>:addPost***");
		System.out.println("size:"+this.postlist.size());
		this.postlist.add(post);
		post.setThread(this);
		this.postCount = this.postCount + 1;
		System.out.println("---<Thread POJO>:addThread---");
		
	}
	
	
	public Thread(String userName, String threadTitle, Long postCount, String openingPost ) {
		this.userName = userName;
		this.threadTitle = threadTitle;
		this.postCount = postCount;
		this.openingPost = openingPost;
	}
	
	public Thread() {
		
	}

	
	@ManyToOne(cascade=CascadeType.REFRESH,optional=true)
	@JoinColumn(name = "forum_id") //this is the column in thread table, and forum_id is a foreign key
	private Forum forum;
	public Forum getForum() {
		return forum;
	}
	public void setForum(Forum forum) {
		this.forum = forum;
	}
	
	
	public Long getThreadId() {
		return threadId;
	}
	public void setThreadId(Long threadId) {
		this.threadId = threadId;
	}
	
	public String getThreadTitle() {
		return threadTitle;
	}
	public void setThreadTitle(String threadTitle) {
		this.threadTitle = threadTitle;
	}
	
	public Date getDateCreated() {
		return dateCreated;
	}
	public void setDateCreated(Date dateCreated) {
		this.dateCreated = dateCreated;
	}
	public String getTags() {
		return tags;
	}
	public void setTags(String tags) {
		this.tags = tags;
	}
	public String getOpeningPost() {
		return openingPost;
	}
	public void setOpeningPost(String openingPost) {
		this.openingPost = openingPost;
	}
	public Long getPostCount() {
		return postCount;
	}
	public void setPostCount(Long postCount) {
		this.postCount = postCount;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}

	
	
}
